{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数学\n",
    "***\n",
    "***\n",
    "Time: 2020-09-07<br>\n",
    "Author: dsy\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 数据质量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.1 期望\n",
    "离散型随机变量的一切可能的取值xi与对应的概率$P_i(x_i)$之积的和称为该离散型随机变量的数学期望（设级数绝对收敛），记为 $E(x)$。随机变量最基本的数学特征之一。它反映随机变量平均取值的大小。又称期望或均值。 若随机变量$X$的分布函数$F(x)$可表示成一个非负可积函数$f(x)$的积分，则称$X$为连续性随机变量，$f(x)$称为$X$的概率密度函数（分布密度函数）。\n",
    "$$\n",
    "E(ax+by+c) = aE(x)+bE(y)+c \\\\\n",
    "如果x和y独立，E(xy)=E(x)E(y)\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2 方差\n",
    "方差是各个数据与平均数之差的平方的平均数。在概率论和数理统计中，方差（英文Variance）用来度量随机变量和其数学期望（即均值）之间的偏离程度。在许多实际问题中，研究随机变量和均值之间的偏离程度有着很重要的意义。 方差刻画了随机变量的取值对于其数学期望的离散程度。 方差深入： 很显然，均值描述的是样本集合的中间点，它告诉我们的信息是很有限的，而标准差给我们描述的则是样本集合的各个样本点到均值的距离之平均。以这两个集合为例，[0，8，12，20]和[8，9，11，12]，两个集合的均值都是10，但显然两个集合差别是很大的，计算两者的标准差，前者是8.3，后者是1.8，显然后者较为集中，故其标准差小一些，标准差描述的就是这种“散布度”。之所以除以n-1而不是除以n，是因为这样能使我们以较小的样本集更好的逼近总体的标准差，即统计上所谓的“无偏估计”。而方差则仅仅是标准差的平方。\n",
    "\n",
    "- ![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91zkdv6ukj308700kweb.jpg)\n",
    "- 如果x和y独立，![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91zl5np3yj308600kt8j.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.3 标准差\n",
    "标准差（Standard Deviation） ，也称均方差（mean square error），是各数据偏离平均数的距离的平均数，它是离均差平方和平均后的方根，用σ表示。标准差是方差的算术平方根。标准差能反映一个数据集的离散程度。平均数相同的，标准差未必相同。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.4 协方差\n",
    "协方差分析是建立在方差分析和回归分析基础之上的一种统计分析方法。 方差分析是从质量因子的角度探讨因素不同水平对实验指标影响的差异。一般说来，质量因子是可以人为控制的。 回归分析是从数量因子的角度出发，通过建立回归方程来研究实验指标与一个（或几个）因子之间的数量关系。但大多数情况下，数量因子是不可以人为加以控制的。\n",
    "在概率论和统计学中，协方差用于衡量两个变量的总体误差。而方差是协方差的一种特殊情况，即当两个变量是相同的情况。\n",
    "\n",
    "- Cov(x,y) = E((x-E(x))(y-E(y)))\n",
    "- Cov(c+ax,d+by) = abCov(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.5 相关系数\n",
    "\n",
    "![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91zq1gbe7j306601ct8k.jpg)\n",
    "\n",
    "\\[-1,1]之间，值越接近1，说明两个变量正相关性（线性）越强。越接近-1，说明负相关性越强，当为0时，表示两个变量没有相关性"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 最大公约数问题\n",
    "#### 2.1 辗转相除法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gcd(a,b):\n",
    "    '''\n",
    "    辗转相除法求解最大公约数\n",
    "    '''\n",
    "    if a < b:\n",
    "        t = a\n",
    "        a = b\n",
    "        b = t\n",
    "    while( 0 != (a% b) ) :\n",
    "        t = a % b\n",
    "        a = b\n",
    "        b = t\n",
    "    return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gcd(12,18)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.2 其他方法\n",
    "- 穷举法\n",
    "- 辗转相减法\n",
    "    * 它的基本原理是：大数减小数，直到两数相等时，即为最大公约数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def g1(a,b):\n",
    "    '''\n",
    "    穷举法\n",
    "    '''\n",
    "    c = min(a,b)\n",
    "    while(True):\n",
    "        if c <= 0:\n",
    "            break\n",
    "        if 0 == a % c and 0 == b % c:\n",
    "            return c\n",
    "        c-=1\n",
    "\n",
    "def g2(a,b):\n",
    "    '''\n",
    "    辗转相减法\n",
    "    '''\n",
    "    while(a != b):\n",
    "        if a > b:\n",
    "            a -= b\n",
    "        else :\n",
    "            b -= a\n",
    "    return a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 牛顿法\n",
    "#### 3.1 迭代公式推导\n",
    "方程$fx)$在$x_{n}$处的切线方程为为$L\n",
    ":Y= f\\prime(x_n)(X-x_n)+f(x_n)$，其与x轴的交点为$x_{n+1}=x_n - \\frac{f(x_n)}{f\\prime(x_n)} \\Leftarrow$迭代公式\n",
    "#### 3.2 牛顿法实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    return x ** 2 - 10\n",
    "def f_(x):\n",
    "    return 2 * x\n",
    "\n",
    "def get_ans2(x=10,iters=10000):\n",
    "    for i in range(iters):\n",
    "        x = x - f(x)/f_(x) # 迭代公式\n",
    "    return x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. 概率密度分布"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.1 均匀分布\n",
    "- 离散随机变量的均匀分布：假设 X 有 k 个取值：x1, x2, ..., xk 则均匀分布的概率密度函数为:\n",
    "![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91ysz3sxsj30aw023glh.jpg)\n",
    "\n",
    "- 连续随机变量的均匀分布：假设 X 在 \\[a, b] 上均匀分布，则其概率密度函数为：\n",
    "![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91ythsdrgj30aa035glj.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.2 伯努利分布\n",
    "伯努利分布：参数为 p∈[0,1]，设随机变量 X ∈ {0,1}，则概率分布函数为：\n",
    "![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91yyoulv0j306m00lmwz.jpg)\n",
    "\n",
    "期望为p，方差为p(1-p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.3 二项分布\n",
    "独立重复地进行 n 次试验中，成功 x 次的概率:\n",
    "![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91z2sy9m5j308800ja9w.jpg)\n",
    "\n",
    "期望为np，方差为np(1-p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.4 高斯分布\n",
    "\n",
    "我们在做模型训练的之后，随机变量取值范围是实数，大多数情况下都假设变量服从高斯分布，原因：\n",
    "- 随机变量大多数情况下有若干个因素组合而成，中心极限定理表明，多个独立随机变量的和近似正态分布\n",
    "- 在具有相同方差的所有可能的概率分布中，正态分布的熵最大（即不确定性最大）；熵大带来的信息量多\n",
    "\n",
    "典型的一维正态分布的概率密度函数为 :\n",
    "\n",
    "![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91z5lcnmcj30dk02fa9z.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.5 拉普拉斯分布\n",
    "\n",
    "概率密度函数：\n",
    "\n",
    "![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91z66211aj309c01hjr9.jpg)\n",
    "\n",
    "期望为u，方差为$2\\gamma^2$\n",
    "\n",
    "拉普拉斯分布比高斯分布更加尖锐和狭窄，在正则化中通常会利用该性质"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.6 泊松分布\n",
    "\n",
    "假设已知事件在单位时间（或者单位面积）内发生的平均次数为λ，则泊松分布描述了：事件在单位时间（或者单位面积）内发生的具体次数为 k 的概率。\n",
    "概率密度函数：\n",
    "\n",
    "![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91z8oplp1j306f01g0sk.jpg)\n",
    "\n",
    "期望：λ，方差为：λ"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. 平面曲线的切线和法线\n",
    "#### 5.1 切线方程\n",
    "![](https://tva1.sinaimg.cn/large/006y8mN6gy1g8xko8hysoj306c0123yi.jpg)\n",
    "\n",
    "#### 5.2 法线方程\n",
    "![](https://tva1.sinaimg.cn/large/006y8mN6gy1g8xkomfj6lj309c01amx9.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. 导数\n",
    "#### 6.1 四则运算\n",
    "- (u+v)'=u'+v'\n",
    "- (u-v)'=u'-v'\n",
    "- (uv)'=u'v+uv'\n",
    "- (u/v)'=(u'v-uv')/v^2\n",
    "\n",
    "#### 6.2 常见导数\n",
    "- y=c(常数),y'=0\n",
    "- y=pow(x,a),y'=a·pow(x,a-1)\n",
    "- y=pow(a,x),y'=pow(a,x)·ln(a)\n",
    "- y=log(a,x),y'=1/(xlna);特别的ln(x)=1/x\n",
    "- y=sin(x),y'=cos(x)\n",
    "- y=cos(x),y'=-sin(x)\n",
    "- y=tan(x),y'=1/(cos(x)^2)\n",
    "\n",
    "#### 6.3 复合函数的运算法则\n",
    "若y=f(g(x)),y'=f'(g(x))·g'(x),前提是g在x处可导，f在g(x)处可导\n",
    "\n",
    "#### 6.4 莱布尼兹公式\n",
    "若u(x),v(x)均n阶可导，则![](https://tva1.sinaimg.cn/large/006y8mN6gy1g8xl2xwg4ij307000udfv.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7. 微分中值定理\n",
    "#### 7.1 费马定理\n",
    "- f在x0的邻域内有定义，且恒满足：f(x)<=f(x0)｜f(x)>=f(x0)\n",
    "- f在x0处可导，则满足f'(x0)=0\n",
    "\n",
    "#### 7.2 拉格朗日中值定理\n",
    "设函数f(x)满足条件：\n",
    "- \\[a,b]上连续\n",
    "- \\(a,b)内可导，则\\(a,b)存在ζ，使得f(b)-f(a)=f'(ζ)(b-a)\n",
    "\n",
    "#### 7.3 柯西中值定理\n",
    "设函数f(x),g(x)满足条件：\n",
    "- \\[a,b]上连续\n",
    "- \\(a,b)内可导，且f'(x)和g'(x)存在，且g'(x)!=0\n",
    "- 则\\(a,b)存在ζ，使得(f(b)-f(a))g'(ζ)=f'(ζ)(g(b)-g(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8. 泰勒公式\n",
    "定义：f(x)在x0处的邻域内有n+1阶的导数，在x0的邻域内的任x，x和x0之间至少存在一个ζ，使得\n",
    "f(x)=f(x0)+f'(x0)(x-x0)+1/2!f''(x0)(x-x0)^2+...+Rn(x)\n",
    "其中，Rn(x) = f<n+1>(ζ)/(n+1)!(x-x0)^(n+1)，为泰勒余项\n",
    "\n",
    "#### 8.1 常见泰勒公式\n",
    "![](https://i.bmp.ovh/imgs/2019/11/0a10d9591cc0c4ac.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 9. 欧拉公式\n",
    "![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91ymfchmgj309a01o0sq.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10. 矩阵\n",
    "#### 10.1 范数\n",
    "1范数：各列绝对值和的最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8],\n",
       "       [ 9, 10, 11, 12],\n",
       "       [13, 14, 15, 16]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "data = np.arange(1,17).reshape((4,-1))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "40.0\n"
     ]
    }
   ],
   "source": [
    "print(np.linalg.norm(data,ord=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2范数：$A^TA$ 矩阵的特征值$λ1,λ2,\\cdots，$然后从所有特征值选出绝对值最大的，最后平方根"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38.62265683187288\n"
     ]
    }
   ],
   "source": [
    "print(np.linalg.norm(data,ord=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 10.2 特征值分解，特征向量\n",
    "特征值分解可以得到特征值与特征向量 \n",
    "特征值表示的是这个特征到底有多重要，而特征向量表示这个特征是什么\n",
    "\n",
    "矩阵A 的特征值与其特征向量![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91zxnzwesj300h00h0rz.jpg), 特征值![](https://tva1.sinaimg.cn/large/006y8mN6gy1g91zy0ybcpj300a00c0pd.jpg)满足：\n",
    "\n",
    "也可写成：![](https://tva1.sinaimg.cn/large/006y8mN6gy1g9201wcuefj303600qa9u.jpg)\n",
    "\n",
    "其中，Q为特征向量组成的矩阵，∑为特征值由大到小组成的矩阵\n",
    "\n",
    "#### 10.3 正定性\n",
    "- 如何判断矩阵的正定性？\n",
    "    - 矩阵的特征值大于等于0，半正定\n",
    "    - 矩阵的特征值大于0，正定\n",
    "\n",
    "- 正定性的用途？\n",
    "    - Hessian矩阵正定性在梯度下降的应用\n",
    "        - 若Hessian正定,则函数的二阶偏导恒大于0，,函数的变化率处于递增状态，判断是否有局部最优解\n",
    "    - 在svm中核函数构造的基本假设\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义:A是n阶方阵，如果对任何非零向量x，都有xTAx>0，其中xT 表示x的转置，就称A正定矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 11. 概率论\n",
    "#### 11.1 条件概率\n",
    "P(A/B) = P(AB)/P(B) \n",
    "\n",
    "#### 11.2 独立\n",
    "P(AB) = P(A)P(B)\n",
    "\n",
    "#### 11.3 概率基础公式\n",
    "\n",
    "加法：P(A+B) = P(A)+P(B)-P(AB)\n",
    "减法：P(A-B) = P(A) - P(AB)\n",
    "乘法：P(AB) = P(A)P(B/A)\n",
    "\n",
    "#### 11.4 全概率：\n",
    "![](https://tva1.sinaimg.cn/large/006y8mN6gy1g920h3l62yj305u00qweb.jpg)\n",
    "\n",
    "#### 11.5 贝叶斯\n",
    "P(B/A)=P(B)*P(A/B)/P(A)\n",
    "\n",
    "#### 11.6 切比雪夫不等式\n",
    "p(|x-u|>k∂)<=1/(k^2),满足k>0,u为期望,∂为标准差\n",
    "\n",
    "绝大多数数据都应该在均值附近\n",
    "\n",
    "#### 11.7 抽球\n",
    "- 有放回的抽取，抽取 m 个排成一列，求不同排列总数：![](https://tva1.sinaimg.cn/large/006y8mN6gy1g920jcfb42j300n00d0s5.jpg)\n",
    "- 无放回的抽取，抽取 m 个排成一列，求不同排列总数:![](https://tva1.sinaimg.cn/large/006y8mN6gy1g920kqhlhbj301w015we9.jpg)\n",
    "\n",
    "#### 11.8 纸牌问题\n",
    "问题：54 张牌，分成 6 份， 每份 9 张牌， 大小王在一起的概率？\n",
    "\n",
    "54张牌分成6等份，共有M=(C54取9)*(C45取9)*...种分法。\n",
    "其中大小王在同一份的分法有N=(C6取1)*(C52取7)*(C45取9)*...种。\n",
    "因此所求概率为P=N / M\n",
    "\n",
    "#### 11.9 棍子/绳子问题\n",
    "问题：一根棍子折三段能组成三角形的概率？\n",
    "\n",
    "假设：棍子长度为1，第一段长度为x， 第二段长度为y， 第三段长度1-x-y \n",
    "\n",
    "分母：总样本空间为： 1 * 1 = 1\n",
    "分子：两边之和大于第三边，得1/8\n",
    "\n",
    "#### 11.10 贝叶斯\n",
    "问题：某城市发生一起汽车撞人逃跑事件，该城市只有两种颜色的车，蓝20%绿80%， 事发时现场只有一个目击者，他指正是蓝车，但根据专家分析，当时那种条件下能看正确的可能性是80%，那么肇事的车是蓝车的概率是多少？ \n",
    "假设事件 A 为目击者指正蓝车， 事件B为肇事车为蓝车，事件C为肇事车为绿车，那么有：\n",
    "\n",
    "0.2`*`0.8/(0.2`*`0.8+0.8`*`0.2)=0.5\n",
    "\n",
    "#### 11.11 选择时间问题\n",
    "问题：一个活动,n个女生手里拿着长短不一的玫瑰花,无序的排成一排,一个男生从头走到尾,试图拿更长的玫瑰花,一旦拿了一朵就不能再拿其他的,错过了就不能回头,问最好的策略及其概率?\n",
    "\n",
    "1/e\n",
    "\n",
    "#### 11.12 0~1均匀分布的随机器如何变化成均值为0，方差为1的随机器\n",
    "\n",
    "均匀分布：\n",
    "E(x) = (a+b)/2\n",
    "标准差：D(x) = (b-a)^2/12\n",
    "\n",
    "所以只需要对x做变换：sqrt(12(x-1/2))即可\n",
    "\n",
    "#### 11.13 抽红蓝球球\n",
    "\n",
    "问题：抽蓝球红球，蓝结束红放回继续，平均结束游戏抽取次数 \n",
    "\n",
    "假设设抽到 蓝球 的概率为 p ， 设抽到红球的概率为 q， 那么抽取到的次数为：1·p+2p·q+...+np·q^(n-1)\n",
    "可得E = p\\[1+2q+...+nq^(n-1)],令1+2q+...+nq^(n-1)=s，再由s为等比公式和s-sq得，E=1/p"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
